/*
    @file kern_init.cl
    @author Claus Wimmer
    @date 2014-07-19

    Project: secondspace-2
    Licence: LGPL (Lesser GPL)
*/


__kernel void corpus(
    __global float *_spaceData,
    __global int *_spaceBits,
    const uint _rows,
    const uint _cols,
    int _prow,
    int _pcol,
    float _val,
    const uint _varsiz) {

    float
        dy = (float)ROW - (float)_prow,
        dx = (float)COL - (float)_pcol,
        d = sqrt(dx * dx + dy * dy),
        phi = atan2(dy, dx);

    if(30.0f > d){
        if(1.0f < d){
            SELF(E) += -1.0f / d * _val;
            SELF(H) += -1.0f / d;
        }else{
            // Avoid division by zero.
            SELF(E) += -1.0f * _val;
            SELF(H) += -1.0f;
        }
    }
}

/*
    Init kernel execution function called by OPENCL.
*/
__kernel void kern_init(
    __global float *_spaceData, // Space data.
    __global int *_spaceBits,   // User bits to mark space pixels.
    __global uchar *_imgData,   // Image area.
    __global uint *_step,       // Each calculation increases the step count by one.
    __global int *_slider,      // User slider to adjust the contrast.
    __global uint *_mouse,      // User's mouse position.
    __global uint *_modus,      // User's radio buttons to switch between three rendering modes.
    const uint _rows,           // Number of pixel rows in space.
    const uint _cols,           // Number of pixel columns in space.
    const uint _layers,         // Number of pixel layers in space.
    const uint _varsiz) {       // Number of variables within one pixel.
    // Begin space (Set an empty not excited space).
    SELF(E)      = 0.0f;
    SELF(H)      = 0.0f;
    SELF(VX)     = 1.0f / 5.0f + sin(COL / 10.0f) / 10.0f;
    SELF(VY)     = 1.0f / 5.0f;
    BIT          = 0x00;
    // Start with a series of corpuses.
    corpus(_spaceData, _spaceBits, _rows, _cols,  200,  50, +1.0f, _varsiz);
    corpus(_spaceData, _spaceBits, _rows, _cols,  200, 150, -1.0f, _varsiz);
    corpus(_spaceData, _spaceBits, _rows, _cols,  200, 250, +1.0f, _varsiz);
    corpus(_spaceData, _spaceBits, _rows, _cols,  200, 350, -1.0f, _varsiz);
    // Render the image.
    show(_spaceData, _spaceBits, _imgData, _slider, _modus, _rows, _cols, _varsiz);
}
